/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"88B77367-CC4B-445B-A700-BFF0D12FE462",variableType:4}
 */
var fAcepta = 0;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"BEB9B662-60ED-4B70-B148-41D742F20653"}
 */
var fProfesion = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"2D858BBC-A195-461D-A44F-36C879572721"}
 */
var fCodPostal = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"B368A7D2-70BA-4853-A29E-C1A8E908FF5A"}
 */
var fDireccion = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"C55EC4A9-9A49-4489-9316-33D9D198EDF2"}
 */
var fPaisDesc = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"BE6623C7-6A0C-47BD-8816-2399FBD721A5",variableType:4}
 */
var fPaisId = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"66867EAD-ADDE-4ECD-83C9-40D958AEAFA5"}
 */
var fCiudad = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"45E84CCD-A9F4-40AF-89A4-487EE7D92761"}
 */
var fApellido = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"931E2B78-016A-4374-800D-65A30578F504"}
 */
var fNombre = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"D18D27C6-8999-42FB-9157-BB786D001F2B",variableType:8}
 */
var fTotal = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"FFED3561-DFAF-4868-BC24-27AB5E1746EE"}
 */
function onShow(firstShow, event) {
	var $total = 0
	for (var i = 1 ; i <= reservas_to_reserva_detalle.getSize() ; i++)
	{
		var $rcd = reservas_to_reserva_detalle.getRecord(i)
		$total += $rcd.precio
	}
	fTotal = $total
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"392832A0-C167-4A97-AD86-98BE1D29197D"}
 */
function confirmar(event) {
	if (fAcepta == 0)
	{
		globals.DIALOGS.showWarningDialog(i18n.getI18NMessage('hoteleria.aviso'),i18n.getI18NMessage('hoteleria.aviso.debeAceptarCondiciones'),'OK')
		return
	}
	if (fNombre != null && fApellido != null && telefono != null && mail != null && fPaisId != null && fCiudad != null)
	{
		cliente_id 	= globals.gClienteId
		usuario 	= globals.gUsuarioId
		fecha 		= globals.gHoy
		pais_id 	= fPaisId
		ciudad		= fCiudad
		direccion 	= fDireccion
		cod_postal 	= fCodPostal
		profesion 	= fProfesion

		for (var i = 1 ; i <= reservas_to_reserva_detalle.getSize() ; i++)
		{
			reservas_to_reserva_detalle.setSelectedIndex(i)
			reservas_to_reserva_detalle.nombre 		= fNombre
			reservas_to_reserva_detalle.apellido 	= fApellido
		}
		
		if (databaseManager.saveData())
		{
			var $digito = globals.generaDigitoVerif(reserva_id.toString())
			var $codigo = reserva_id * 10 + $digito
			codigo_interno = utils.numberFormat($codigo, '000000000000000')
			var $id = reserva_id
			estado = 1
			databaseManager.saveData()
			
/*			if (llamarWebService())
			{
				if (estado == 2)
				{*/
					limpiarVariables()
					forms.reserva_5.controller.loadRecords($id)
					forms.reserva_1.reiniciarBusqueda()
					globals.show('reserva_5')
					/*	}
			}
			else
			{
				globals.DIALOGS.showErrorDialog(i18n.getI18NMessage('hoteleria.aviso'),i18n.getI18NMessage('hoteleria.error.webService'),'OK')
			}*/
		}
		else
		{
			globals.DIALOGS.showWarningDialog(i18n.getI18NMessage('hoteleria.aviso'),i18n.getI18NMessage('hoteleria.aviso.errorConfirma'),'OK')
		}
	}
	else
		globals.DIALOGS.showWarningDialog(i18n.getI18NMessage('hoteleria.aviso'),i18n.getI18NMessage('hoteleria.aviso.llenarDatos'),'OK')
}

/**
 * @properties={typeid:24,uuid:"FD37E02F-781D-4902-85B6-3352896E4A9B"}
 */
function limpiarVariables() {
	fNombre 	= null
	fApellido 	= null
	fPaisId 	= null
	fCiudad 	= null
	fDireccion	= null
	fProfesion	= null
	fCodPostal	= null
	fAcepta		= 0
}

/**
 * @properties={typeid:24,uuid:"2539CF59-B11D-4BFB-916D-9A2A3A596684"}
 */
function llamarWebService()
{
	var $client = plugins.http.createNewHttpClient()
	$client.setTimeout(30000) //30 segundos
	var $post = $client.createPostRequest('http://servoy.infocomsa.com:8080/servoy-service/rest_ws/hoteleria_ws/ws_reservas/'+codigo_interno)
	$post.addHeader('Content-type','application/json; charset=UTF-8')
	var vJSON = plugins.serialize.toJSON("{'moneda':'Pesos', 'fechahora':'11/11/2013 12:33','codautorizacion':'0A435252','cuotas':1,'titular':'Juan Perez','monto':100.20,'tarjeta':'Visa','emailcomprador':'info@infocomsa.com','noperacion':"+codigo_interno+",'resultado':'APROBADA','tipodocdescri':'DNI','tipodocdescri':'DNI','nrodoc':'33597523'}")
	$post.addParameter(null,vJSON)

	var $response = $post.executeRequest()
	if ($response.getStatusCode() != 200)
	{
		return false
	}
	databaseManager.refreshRecordFromDatabase(foundset,foundset.getSelectedIndex())
	return true
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"B4718637-DFF2-48E9-BDC3-B4C563C84632"}
 */
function onDataChangePais(oldValue, newValue, event) {
	fPaisDesc = application.getValueListDisplayValue('paises',newValue)
	elements.fld_fciudad.requestFocus()
	return true
}
